using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;
using Fieldbook.Common;

namespace Fieldbook.DAL
{
    public static class OperationDAO
    {
        #region insert
        private static DbCommand GetInsertCommand(SqlDatabase db)
        {
            string insertSQL = "INSERT INTO dbo.Operation (CATEGORYID, DESCRIPTION, HOUSEHOLDID, OPERATIONTYPE) VALUES (@CATEGORYID, @DESCRIPTION, @HOUSEHOLDID, @OPERATIONTYPE) SET @OPERATIONID = SCOPE_IDENTITY()";
            DbCommand insertCommand = db.GetSqlStringCommand(insertSQL);
            db.AddOutParameter(insertCommand, "@OPERATIONID", DbType.Int64, 8);
            db.AddInParameter(insertCommand, "@CATEGORYID", DbType.Int64);
            db.AddInParameter(insertCommand, "@DESCRIPTION", DbType.String);
            db.AddInParameter(insertCommand, "@HOUSEHOLDID", DbType.Int64);
            db.AddInParameter(insertCommand, "@OPERATIONTYPE", DbType.Byte);
            return insertCommand;
        }
        private static void SetInsertValues(SqlDatabase db, DbCommand cmd, Operation entity)
        {
            db.SetParameterValue(cmd, "@CATEGORYID", entity.CategoryID);
            db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
            db.SetParameterValue(cmd, "@HOUSEHOLDID", entity.HouseHoldID);
            db.SetParameterValue(cmd, "@OPERATIONTYPE", (byte)entity.OperationTypeID);
        }
        public static bool InsertOperation(Operation entity)
        {
            bool res = false;
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
            if (res)
            {
                entity.OperationID = (long?)db.GetParameterValue(cmd, "@OPERATIONID");
            }
            return res;
        }        
        #endregion

        #region select
        private static List<Operation> LoadOperations(SqlDatabase db, DbCommand command)
        {
            List<Operation> res = new List<Operation>();
            IDataReader reader = null;
            try
            {
                reader = db.ExecuteReader(command);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        Operation entity = new Operation();
                        entity.OperationID = reader.GetInt64(0);
                        entity.CategoryID = reader.GetInt64(1);
                        entity.Description = reader.GetString(2);
                        entity.HouseHoldID = reader.IsDBNull(3) ? null : (long?)reader.GetInt64(3);
                        entity.OperationTypeID = (Operation.OperationType)reader.GetByte(4);
                        res.Add(entity);
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                res = null;
                throw ex;
            }
            return res;
        }

        public static List<Operation> LoadOperations(long userID, Operation.OperationType operationType)
        {
            string selectSQL = @"SELECT O.OperationID, O.CategoryID, O.Description, O.HouseHoldID, O.OperationType 
FROM OperationCategory OC 
LEFT OUTER JOIN HouseHold HH ON OC.HOUSEHOLDID = HH.HEADID OR OC.HOUSEHOLDID IS NULL
JOIN Operation O ON OC.CATEGORYID = O.CATEGORYID AND (O.HOUSEHOLDID = HH.HEADID OR O.HOUSEHOLDID IS NULL)
WHERE HH.MemberID = @MEMBERID AND @OPERATIONTYPE = @OPERATIONTYPE";
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = db.GetSqlStringCommand(selectSQL);
            db.AddInParameter(cmd, "@MEMBERID", DbType.Int64, userID);
            db.AddInParameter(cmd, "@OPERATIONTYPE", DbType.Byte, (Byte)operationType);
            return LoadOperations(db, cmd);
        }

        public static List<Operation> LoadOperations(long userID, long categoryID, Operation.OperationType operationType)
        {
            string selectSQL = @"SELECT O.OperationID, O.CategoryID, O.Description, O.HouseHoldID, O.OperationType 
FROM OperationCategory OC 
LEFT OUTER JOIN HouseHold HH ON OC.HOUSEHOLDID = HH.HEADID OR OC.HOUSEHOLDID IS NULL
JOIN Operation O ON OC.CATEGORYID = O.CATEGORYID AND (O.HOUSEHOLDID = HH.HEADID OR O.HOUSEHOLDID IS NULL)
WHERE HH.MemberID = @MEMBERID AND O.CATEGORYID = @CATEGORYID AND @OPERATIONTYPE = @OPERATIONTYPE";
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = db.GetSqlStringCommand(selectSQL);
            db.AddInParameter(cmd, "@MEMBERID", DbType.Int64, userID);
            db.AddInParameter(cmd, "@CATEGORYID", DbType.Int64, categoryID);
            db.AddInParameter(cmd, "@OPERATIONTYPE", DbType.Byte, (Byte)operationType);
            return LoadOperations(db, cmd);
        }
        #endregion
    }
}
